package cn.edu.swufe.stu2025.finalwork;

import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

class noteViewHolder extends RecyclerView.ViewHolder{
    TextView title,content;

    final private String TAG = "noteViewHolder";
    public noteViewHolder(@NonNull View itemView) {
        super(itemView);
        // 获取数据
        title=itemView.findViewById(R.id.title);
        content=itemView.findViewById(R.id.content);
        Log.i(TAG, "noteViewHolder title:"+ title + "content:" + content) ;
    }
}

public class noteAdapter extends RecyclerView.Adapter<noteViewHolder> {
    private List<note> notes;
    final private String TAG = "noteAdapter";
    private Set<Integer> selectedPositions = new HashSet<>();
    private boolean multiSelectMode = false;
    public noteAdapter(List<note> notes){
        this.notes=notes;
        Log.i(TAG, "noteAdapter notes:"+ notes);
    }

    @NonNull
    @Override
    public noteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note,parent,false);
        Log.i(TAG, "onCreateViewHolder: noteViewHolder");
        return new noteViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull noteViewHolder holder, int position) {
        note not = notes.get(position);
        holder.title.setText(not.getTitle());
        holder.content.setText(not.getContent());
        // 始终根据选中状态设置背景色
        holder.itemView.setBackgroundColor(
                isSelected(position) ? Color.LTGRAY : Color.WHITE
        );
        holder.itemView.setOnLongClickListener(v -> {
            if (!multiSelectMode) {
                multiSelectMode = true;
                selectedPositions.clear();
                selectedPositions.add(position);
                notifyDataSetChanged();
                if (multiSelectListener != null) multiSelectListener.onMultiSelectModeChanged(true);
            }
            return true;
        });
        holder.itemView.setOnClickListener(v -> {
            if (multiSelectMode) {
                toggleSelection(position);
            }
        });
    }

    public void toggleSelection(int position) {
        if (selectedPositions.contains(position)) {
            selectedPositions.remove(position);
        } else {
            selectedPositions.add(position);
        }
        notifyItemChanged(position); // 只刷新当前项
    }
    public void exitMultiSelectMode() {
        multiSelectMode = false;
        selectedPositions.clear();
        notifyDataSetChanged();
        if (multiSelectListener != null) multiSelectListener.onMultiSelectModeChanged(false);
    }
    public boolean isMultiSelectMode() {
        return multiSelectMode; // multiSelectMode 为你管理多选状态的变量
    }
    public boolean isSelected(int position) {
        return selectedPositions.contains(position);
    }
    public Set<Integer> getSelectedPositions() {
        return selectedPositions;
    }
    public void deleteSelectedNotes() {
        // 注意：要从大到小删除，避免下标错乱
        List<Integer> toDelete = new java.util.ArrayList<>(selectedPositions);
        java.util.Collections.sort(toDelete, java.util.Collections.reverseOrder());
        for (int pos : toDelete) {
            notes.remove(pos);
        }
        exitMultiSelectMode();
        notifyDataSetChanged();
    }

    // 监听多选模式变化
    public interface MultiSelectListener {
        void onMultiSelectModeChanged(boolean enabled);
    }
    private MultiSelectListener multiSelectListener;
    public void setMultiSelectListener(MultiSelectListener listener) {
        this.multiSelectListener = listener;
    }


    @Override
    public int getItemCount() {
        Log.i(TAG, "getItemCount: "+notes.size());
        return notes.size();
    }

}
